using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Layout;

namespace RAMain
{
    public partial class Form1 : XtraForm
    {
        static RADB.DataSetRA staticDataSetRA = new RADB.DataSetRA();
        static Dictionary<string, object> tableAdapters = new Dictionary<string, object>();        

        public Form1()
        {
            this.dataSetRA = staticDataSetRA;
            InitializeComponent();
            this.dataSetRA = DataTransferObj.dataSet;
            gridControlMenu.DataSource = this.dataSetRA;
            gridControlEmployees.DataSource = this.dataSetRA;
            gridControlSessions.DataSource = this.dataSetRA;
            tabControlContent.ItemSize = new System.Drawing.Size(0, 1);
            
            SetBingdingSource();

            InitChartControl();
        }
        void InitChartControl()
        {
            //chartControl.AppearanceName = "Light";
            //chartControl.PaletteName = "Office";
            //chartControl.Series.Clear();
            //ChangeSeries(ViewType.Bar);
            //ChangeSeries(ViewType.Bar);
            //chartControl.Legend.Visible = false;
            //if (chartControl.Series[0].Label != null)
            //    chartControl.Series[0].Label.Visible = true;
        }
        void ChangeSeries(ViewType viewType)
        {
            //double[] values = { 3.1, 2.3, 3.2, 3.9, 5.1 };
            //Series series = new Series("Series 1", viewType);
            //for (int i = 0; i < values.Length; i++)
            //    series.Points.Add(new SeriesPoint((i + 1) * 10, values[i]));
            
            //chartControl.Series.Add(series);
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
            foreach (DevExpress.XtraNavBar.NavBarItem i in navBarControl.Items)
            {
                i.Appearance.Options.UseFont = true;
            }
            cbEmployeeType.SelectedIndex = 0;

            //get users
            tableAdapters.Add("EmployeeRole", employeeRoleTableAdapter);
            employeeRoleTableAdapter.Fill(dataSetRA.EmployeeRole);
            tableAdapters.Add("Employees", employeesTableAdapter);
            employeesTableAdapter.Fill(dataSetRA.Employees);
            
            //login
            Login();

            //XtraForm1 tmp = new XtraForm1();
            //tmp.Show();

            
            //
            LoadData();
            OpenServer();
            tablesManagement.SetSource(this.dataSetRA, this.tablesTableAdapter);
            
            //
            SetEvent();
        }
      

        private void CommonItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            foreach (DevExpress.XtraNavBar.NavBarItem i in navBarControl.Items)
            {
                i.Appearance.Options.UseFont = true;
            }
            DevExpress.XtraNavBar.NavBarItem item = sender as DevExpress.XtraNavBar.NavBarItem;
            item.Appearance.Options.UseFont = false;
                        
            string tabName = item.Name;
            tabName = "page" + tabName.Remove(tabName.Length-4);
            tabControlContent.SelectedTab = tabControlContent.TabPages[tabName];
        }

        private void changePasswordItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ChangePassword();
        }

        private void ChangePassword()
        {
            var frm = new ChangePassDlg();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("Change Password");
            }
        }

        private void logoutItem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            var frm = new LogoutDialog();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
            {
                Application.Restart();
            }
        }


        private void iAbout_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            tabControlContent.SelectedTab = pagePatronTraffic2;
        }

        private void layoutViewMenu_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            productsTableAdapter.Update(dataSetRA);
        }

        private void repositoryCategories_View_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            categoriesTableAdapter.Update(dataSetRA.Categories);
        }

        private void gridViewEmployees_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            employeesTableAdapter.Update(dataSetRA.Employees);
        }

        private void gridViewOrders_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            sessionsTableAdapter.Update(dataSetRA.Sessions);
        }

        private void gridViewOrderItems_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            orderItemsTableAdapter.Update(dataSetRA.OrderItems);
        }

        private void gridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                GridView view = sender as GridView;
                if (view.FocusedRowHandle < 0)
                    return;

                if (MessageBox.Show("Are you sure you wish to delete the selected item(s) ?", "Confirmation", MessageBoxButtons.YesNo) != DialogResult.Yes)
                    return;

                view.DeleteSelectedRows();

            }
        }

        private void layoutView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {

                LayoutView view = sender as LayoutView;

                if (view.FocusedRowHandle < 0)
                    return;

                if (MessageBox.Show("Are you sure you wish to delete the selected item(s) ?", "Confirmation", MessageBoxButtons.YesNo) != DialogResult.Yes)
                    return;
               
                view.DeleteSelectedRows();

            }
        }

        private void tablesManagement_TableDoubleClick(int tableid, _TablesManagement.Table.TableStatus status)
        {
            //MessageBox.Show(dataSetRA._Tables.FindByID(tableid).TableName);
        }

        private void gridControl_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (e.Button.ButtonType == NavigatorButtonType.Remove)
            {
                if (MessageBox.Show("Are you sure you wish to delete the selected item(s) ?", "Confirmation", MessageBoxButtons.YesNo) != DialogResult.Yes)
                {
                    e.Handled = true;
                    return;
                }
            }
        }

        private void FilterOption_FilterChanged(object sender, EventArgs e)
        {
            if (PatronFilterOption.SummaryType == ChartFilterControl.ChartFilterControl.SummaryOption.bytheDay)
            {
                int[] res = patronStatisticsByDay(PatronFilterOption.StartTime, PatronFilterOption.FinishTime);
                if (res == null)
                {
                    return;
                }
                chartPatronTraffic.Series[0].Points.Clear();
                chartPatronTraffic.Series[0].ArgumentScaleType = ScaleType.DateTime;
                chartPatronTraffic.Series[0].PointOptions.ValueNumericOptions.Precision = 0;


                int numOfDays = (PatronFilterOption.FinishTime - PatronFilterOption.StartTime).Days + 1;
                for (int i = 0; i < numOfDays; i++)
                {
                    chartPatronTraffic.Series[0].Points.Add(new SeriesPoint(PatronFilterOption.StartTime.Date.AddDays(i), res[i]));
                }
            }
            else
            {
                double[] res = patronStatisticsByHours(PatronFilterOption.StartTime, PatronFilterOption.FinishTime);
                if (res == null)
                {
                    return;
                }
                //chartPatronTrafficHour.Series.Clear();
                //Series series = new Series("Num of Patron", ViewType.Bar);
                chartPatronTraffic.Series[0].Points.Clear();
                chartPatronTraffic.Series[0].ArgumentScaleType = ScaleType.Numerical;
                chartPatronTraffic.Series[0].PointOptions.ValueNumericOptions.Precision = 1;
                
                for (int i = 0; i < res.Length; i++)
                {
                    chartPatronTraffic.Series[0].Points.Add(new SeriesPoint(i, res[i]));
                }
            }
        }

        private void RevenueFilterOption_FilterChanged(object sender, EventArgs e)
        {
            if (RevenueFilterOption.SummaryType == ChartFilterControl.ChartFilterControl.SummaryOption.bytheDay)
            {
                double[] res = RevenueStatisticsByDay(RevenueFilterOption.StartTime, RevenueFilterOption.FinishTime);
                if (res==null)
                {
                    return;
                }
                chartRevenue.Series[0].Points.Clear();
                chartRevenue.Series[0].ArgumentScaleType = ScaleType.DateTime;

                int numOfDays = (RevenueFilterOption.FinishTime - RevenueFilterOption.StartTime).Days + 1;
                for (int i = 0; i < numOfDays; i++)
                {
                    chartRevenue.Series[0].Points.Add(new SeriesPoint(RevenueFilterOption.StartTime.Date.AddDays(i), res[i]));
                }
            }
            else
            {
                double[] res = RevenueStatisticsByHours(RevenueFilterOption.StartTime, RevenueFilterOption.FinishTime);
                if (res==null)
                {
                    return;
                }
                chartRevenue.Series[0].Points.Clear();
                chartRevenue.Series[0].ArgumentScaleType = ScaleType.Numerical;

                for (int i = 0; i < res.Length; i++)
                {
                    chartRevenue.Series[0].Points.Add(new SeriesPoint(i, res[i]));
                }
            }
        }

        private void MenuPopFilterOption_FilterChanged(object sender, EventArgs e)
        {
            Dictionary<int, int> res = MenuItemPopularityByDay(MenuPopFilterOption.StartTime, MenuPopFilterOption.FinishTime);
            if (res==null)
            {
                return;
            }

            chartMenuPop.Series[0].Points.Clear();
            foreach (KeyValuePair<int, int> item in res)
            {
                chartMenuPop.Series[0].Points.Add(new SeriesPoint(dataSetRA.Products.FindByID(item.Key).Name, item.Value));
            }
        }

        private void MenuPopTopEdit_ValueChanged(object sender, EventArgs e)
        {
            if ((decimal)MenuPopTopEdit.EditValue > 1)
            {
                //chartMenuPop.Series[0].TopNOptions.Mode = TopNMode.
                chartMenuPop.Series[0].TopNOptions.Count = (int)((decimal)MenuPopTopEdit.EditValue); 
            }
        }

        private void tunrAroundFilterOption_FilterChanged(object sender, EventArgs e)
        {
            double[] res = TurnAroundTimeByDay(tunrAroundFilterOption.StartTime, tunrAroundFilterOption.FinishTime);
            if (res==null)
            {
                return;
            }

            chartTurnAround.Series[0].Points.Clear();

            int numOfDays = (tunrAroundFilterOption.FinishTime - tunrAroundFilterOption.StartTime).Days + 1;
            for (int i = 0; i < numOfDays; i++)
            {
                chartTurnAround.Series[0].Points.Add(new SeriesPoint(tunrAroundFilterOption.StartTime.Date.AddDays(i), res[i]));
            }
        }

        private void PreFilterOption_FilterChanged(object sender, EventArgs e)
        {
            double[] res = PreparationTimeByDay(PreFilterOption.StartTime, PreFilterOption.FinishTime);
            if (res == null)
            {
                return;
            }

            chartPre.Series[0].Points.Clear();

            int numOfDays = (PreFilterOption.FinishTime - PreFilterOption.StartTime).Days + 1;
            for (int i = 0; i < numOfDays; i++)
            {
                chartPre.Series[0].Points.Add(new SeriesPoint(PreFilterOption.StartTime.Date.AddDays(i), res[i]));
            }
        }

        private void effFilterOption_FilterChanged(object sender, EventArgs e)
        {
            Dictionary<int, int[]> res = WaiterEffciency(effFilterOption.StartTime, effFilterOption.FinishTime);
            if (res==null)
            {
                return;
            }
            chartEff.Series[0].Points.Clear();
            foreach (KeyValuePair<int, int[]> item in res)
            {
                chartEff.Series[0].Points.Add(new SeriesPoint(dataSetRA.Employees.FindByID(item.Key).FirstName, item.Value[0]+ item.Value[1]));
            }
        }        

    }
}